#!/bin/zsh
# Enhanced LLM Bridge Voice Control Node 简化启动脚本
# 适用于 macOS，避免 Broken Pipe 错误
#
# 使用方法:
#   ./start_voice_node_simple.sh

echo "🚀 Enhanced LLM Bridge Voice Control Node (简化版)"
echo "============================================================"

# 检测操作系统
OS=$(uname -s)
echo "🖥️  操作系统: $OS"
echo "🐚 Shell: $SHELL"

# 检查ROS2环境
if ! command -v ros2 &> /dev/null; then
    echo "❌ ROS2 未找到，请确保已安装并激活ROS2环境"
    exit 1
fi

echo "✅ ROS2 环境检查通过"

# 构建包
echo ""
echo "🔨 构建 fishbot_microros_voice 包..."
colcon build --packages-select fishbot_microros_voice

if [ $? -eq 0 ]; then
    echo "✅ 包构建成功"
else
    echo "❌ 包构建失败"
    exit 1
fi

# 配置环境 (macOS使用zsh)
echo ""
echo "🔧 配置环境..."
source install/setup.zsh
echo "✅ 环境配置完成"

# 检查可执行文件是否存在
echo ""
echo "📦 检查可执行文件..."
if [ -f "./install/fishbot_microros_voice/bin/llm_bridge_voice_control_node" ]; then
    echo "✅ 可执行文件存在"
else
    echo "❌ 可执行文件不存在"
    exit 1
fi

# 显示启动信息
echo ""
echo "🎯 增强版语音控制节点特性:"
echo "   📡 服务器: ws://192.168.2.28:8060/ws/robot"
echo "   🛡️ 安全: 命令超时、紧急停止、速度限制"
echo "   🎤 LLM: MessagePack/JSON双格式、智能消息处理"
echo "   🔧 参数: 完整的ROS2参数系统"
echo "   📊 监控: 性能监控和状态发布"
echo ""
echo "📡 ROS2话题:"
echo "   - /cmd_vel (机器人运动控制)"
echo "   - /voice_text (语音文本)"
echo "   - /structured_voice_command (结构化命令)"
echo "   - /voice_control_status (状态信息)"
echo "   - /voice_control/status (兼容状态)"
echo "   - /voice_control/feedback (语音反馈)"
echo "   - /voice_control/mode (控制模式)"
echo ""
echo "🚀 启动节点... (按 Ctrl+C 停止)"
echo "============================================================"

# 启动节点 - 直接使用可执行文件路径
./install/fishbot_microros_voice/bin/llm_bridge_voice_control_node

echo ""
echo "👋 节点已停止"